<!-- 正文开始 -->
<style>
  .companyFormPhotos {
    /* margin-left:140px; */
    line-height: 108px;
    float: left;
    margin-right: 5px;
  }

  .layui-table-cell .layui-table-sort {
    display: none;
  }
  .maintenanceForm .layui-form-label {
    width: 85px;
  }
  .maintenanceForm .layui-input-block {
    margin-left: 115px;
  }
</style>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <!-- 处置部门 -->
                <div class="layui-inline">
                  <label class="layui-form-label">处置部门：</label>
                  <div class="layui-input-inline">
                    <input
                      id="departmentSearchMainId"
                      type="hidden"
                      name="departmentSearchMainId"
                    />
                    <div id="departmentSearchMain"></div>
                  </div>
                </div>
                <!-- 责任岗位 -->
                <div class="layui-inline">
                  <label class="layui-form-label">责任岗位：</label>
                  <div class="layui-input-inline">
                    <select
                      id="postIdSearchClaim"
                      name="postIdSearchClaim"
                      lay-filter="postIdSearchClaim"
                      class="common-select-search"
                    ></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label">状态：</label>
                  <div class="layui-input-inline">
                    <select
                      name="statusSearch_main"
                      lay-filter="statusSearch_main"
                      class="common-select-search"
                    >
                      <option value="">请选择状态</option>
                      <option value="1">待认领</option>
                      <option value="2">已认领</option>
                      <option value="3">完成</option>
                    </select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">任务编码：</label>
                  <div class="layui-input-inline">
                    <input
                      name="taskNo"
                      id="taskNo"
                      class="layui-input"
                      type="text"
                      placeholder="请输入任务编码"
                      autocomplete="off"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">录入人：</label>
                  <div class="layui-input-inline">
                    <input
                      name="createName"
                      id="createName"
                      class="layui-input"
                      type="text"
                      placeholder="请输入录入人"
                      autocomplete="off"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">认领人：</label>
                  <div class="layui-input-inline">
                    <input
                      name="claimName"
                      id="claimName"
                      class="layui-input"
                      type="text"
                      placeholder="请输入认领人"
                      autocomplete="off"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">设备清单编号：</label>
                  <div class="layui-input-inline">
                    <input
                      name="deviceNo"
                      class="layui-input"
                      type="text"
                      placeholder="请输入设备清单编号"
                      autocomplete="off"
                    />
                  </div>
                </div>
              </div>
              <div class="query-rt">
                <div class="layui-inline">
                  <button
                    class="layui-btn icon-btn color-green"
                    lay-filter="formSubSearchLog_main"
                    lay-submit
                    layui-form-keyDownSearch="true"
                  >
                    查询
                  </button>
                  <button
                    class="layui-btn icon-btn color-white"
                    lay-filter="resets"
                    lay-submit
                  >
                    重置
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button
                id="maintenanceAdd"
                class="layui-btn icon-btn color-reseda"
              >
                添加
              </button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="maintenanceTableInfo"
        lay-filter="maintenanceTableInfo"
      ></table>
    </div>
  </div>
</div>

<script type="text/html" id="viewAttachmentHtml">
  <div id="example1" style="height: 100%;"></div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="maintenanceTableBar">
  <!-- 修改 只有在 刚创建+未审核 or 待领取+未审核 时才能被修改 被领取或者完成后 就不能修改了 -->
  <!-- {{# if((d.status =='1' && d.examineType =='0') || (d.status == '3' && d.examineType == '0')) {  }} -->
  <a class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="edit">修改</a>
  <!-- {{# } }} -->
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
    >详情</a
  >
  <!-- 只有未审的能够审核 -->
  <!-- {{# if((d.examineType =='0' && d.status =='1') || (d.examineType =='0' && d.status =='3')) {  }} -->
  <a class="layui-btn layui-btn-danger  layui-btn-xs" lay-event="audit">审核</a>
  <!-- {{# } }} -->

  <!-- 只有待认领的才能认领 并且审核通过 -->
  <!-- {{# if(d.status =='1' && d.examineType == '1') {  }} -->
  <a class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="claim">认领</a>
  <!-- {{# } }} -->
</script>

<!-- 表单弹窗修改/添加 -->
<script type="text/html" id="maintenanceForm">
  <form
    lay-filter="maintenanceForm"
    class="layui-form maintenanceForm model-form"
  >
    <input name="id" type="hidden" />
    <div class="layui-form-item layui-row">
      <div class="layui-col-md3">
        <label class="layui-form-label">任务类型：</label>
        <input id="taskType" type="hidden" value="1" />
        <div class="layui-input-block">
          <div style="padding-top: 9px;">
            <span style="color: red">认领任务</span>
          </div>
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">设备清单号：</label>
        <div class="layui-input-block">
          <input
            id="deviceNo"
            name="deviceNo"
            type="text"
            class="layui-input"
          />
          <input
            id="deviceId"
            name="deviceId"
            type="hidden"
            class="layui-input"
          />

          <!-- <select lay-search="" id="deviceNoShowEdit" name="deviceNo" lay-filter="deviceNo"
                            class="common-select-search">
                        <option value="">请选择设备清单号</option>
                    </select> -->
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">设备类型：</label>
        <div class="layui-input-block">
          <input id="deviceLX" type="text" class="layui-input" disabled />
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">设备分类：</label>
        <div class="layui-input-block">
          <input id="deviceFL" type="text" class="layui-input" disabled />
        </div>
      </div>
    </div>
    <div class="layui-form-item layui-row">
      <div class="layui-col-md3">
        <label class="layui-form-label">所属产线：</label>
        <div class="layui-input-block">
          <input id="deviceCX" type="text" class="layui-input" disabled />
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">设备型号：</label>
        <div class="layui-input-block">
          <input id="deviceModelId" type="hidden" class="layui-input"  />
          <input id="deviceModelName" type="text" class="layui-input" disabled />
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">任务性质：</label>
        <div class="layui-input-block">
          <select
            id="toolDeviceType"
            name="taskNature"
            lay-filter="taskNature"
            class="common-select-search"
          ></select>
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">维保类型：</label>
        <div class="layui-input-block">
          <select
            id="maintenanceType"
            name="maintenanceType"
            lay-filter="maintenanceType"
            class="common-select-search"
          ></select>
        </div>
      </div>

   
    </div>
    <div class="layui-form-item layui-row">
      <div class="layui-col-md3">
        <label class="layui-form-label">维保点位：</label>
        <div class="layui-input-block">
          <input
            id="deviceLocation"
            name="deviceLocation"
            placeholder="请输入维保点位"
            type="text"
            class="layui-input"
            required
            autocomplete="off"
          />
        </div>
      </div>
      <div class="layui-col-md3">
        <label class="layui-form-label">处置部门：</label>
        <div class="layui-input-block">
          <input
            id="departmentAddMainId"
            name="departmentAddMainId"
            type="text"
            class="layui-input"
            disabled
          />
        </div>
      </div>

      <div class="layui-col-md3">
        <label class="layui-form-label">责任岗位：</label>
        <div class="layui-input-block">
          <input
            id="postIdAddDetail"
            name="postIdAddDetail"
            type="text"
            class="layui-input"
            disabled
          />
        </div>
      </div>

      <div class="layui-col-md3">
        <label class="layui-form-label">任务名称：</label>
        <div class="layui-input-block">
          <div style="padding-top: 9px;">
            <span style="color: red" id="sbqdbh">设备清单编号</span>
            <span style="color: red" id="dwh">-点位号</span>
            <span style="color: red" id="sbmc">-设备名称</span>
            <span style="color: red" id="rwlx">-维保认领任务</span>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-form-item layui-row">
      <div class="layui-col-md3">
        <label class="layui-form-label">实施前图片：</label>
        <div class="layui-input-block">
          <div id="ProPic_path" class="companyFormPhotos"></div>
          <div id="ProPicBtn" class="layui-btn" style="float: left;">
            <i class="layui-icon">&#xe681;</i>上传图片
          </div>
          <input
            name="preImg"
            type="hidden"
            class="layui-input"
            lay-verify=""
            id="preImg"
          />
        </div>
      </div>
      <div class="layui-col-md12">
        <label class="layui-form-label">任务备注：</label>
        <div class="layui-input-block">
          <textarea
            name="taskRemark"
            placeholder="请输入任务备注"
            class="layui-textarea"
            autocomplete="off"
          />
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button
        class="layui-btn layui-btn-primary"
        type="button"
        ew-event="closeDialog"
      >
        取消
      </button>
      <button
        class="layui-btn"
        lay-filter="maintenanceTypeFormSubmit"
        lay-submit
      >
        保存
      </button>
    </div>
  </form>
</script>

<!-- 审核窗口 -->
<script type="text/html" id="maintenanceSHForm">
  <form
    lay-filter="maintenanceSHForm"
    class="layui-form model-form"
    style="padding: 5px 30px 0;height: calc(100% - 5px);overflow: hidden;"
  >
    <div
      class="open-details-box"
      style="height: calc(100% - 100px); padding-top: 20px; overflow-y:auto;margin-bottom: 20px"
    >
      <div class="examineProductionPlanForm-title">
        <h3 class="title">审核信息：</h3>
      </div>
      <div class="line"></div>
      <br />
      <div class="layui-form-item">
        <label class="layui-form-label">审核结果：</label>
        <div class="layui-input-block">
          <select
            name="checkStatus"
            lay-filter="checkStatus"
            class="common-select-search"
          >
            <option value="approve">审核通过</option>
            <option value="reject">驳回</option>
          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">审核备注：</label>
        <div class="layui-input-block">
          <textarea
            name="remark"
            placeholder="请输入审核备注"
            class="layui-textarea"
            autocomplete="off"
          ></textarea>
        </div>
      </div>
    </div>
    <div class="layui-form-item text-right">
      <button
        class="layui-btn layui-btn-primary"
        type="button"
        ew-event="closeDialog"
      >
        取消
      </button>
      <button class="layui-btn" lay-filter="MaintenanceShFormSubmit" lay-submit>
        提交
      </button>
    </div>
  </form>
</script>

<!-- 详情 -->
<script type="text/html" id="maintenanceDetailForm">
  <form lay-filter="maintenanceDetailForm" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto">
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">任务编号：</label>
            <div class="layui-input-block">
              <input
                class="layui-input"
                style="color: red"
                id="taskNoDetail"
                disabled
              />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">任务名称：</label>
            <div class="layui-input-block">
              <input
                class="layui-input"
                style="color: red"
                id="taskNameDetail"
                disabled
              />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">任务类型：</label>
            <div class="layui-input-block">
              <input
                class="layui-input"
                style="color: red"
                id="taskTypeNameDetail"
                value="1"
              />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 84px;"
              >设备清单号：</label
            >
            <div
              class="layui-input-block"
              style="width: 77%;margin-left: 114px"
            >
              <input class="layui-input" id="deviceNoDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">设备类型：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="deviceLXDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">设备分类：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="deviceFLDetail" disabled />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">所属产线：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="deviceCXDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">任务性质：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="taskNatureDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">维保类型：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="maintenanceTypeDetail" disabled />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">维保点位：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="deviceLocationDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">处置部门：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="departmentIdDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">责任岗位：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="postIdDetail" disabled />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 84px"
              >实施前图片：</label
            >
            <div class="layui-input-block">
              <div id="proImage" class="companyFormPhotos"></div>
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 84px"
              >实施后图片：</label
            >
            <div class="layui-input-block">
              <div id="postImage" class="companyFormPhotos"></div>
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 98px"
              >用料总金额：</label
            >
            <div
              class="layui-input-block"
              style="width: 74%;margin-left: 128px;"
            >
              <input
                class="layui-input"
                style="color: red"
                id="totalMoney"
                disabled
              />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md12">
            <label class="layui-form-label">用料明细：</label>
            <div class="layui-input-block">
              <table
                class="layui-table"
                id="maintenanceExecuteDetailInfo"
                lay-filter="maintenanceExecuteDetailInfo"
              ></table>
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md6">
            <label class="layui-form-label">任务备注：</label>
            <div class="layui-input-block">
              <textarea
                id="taskRemarkDetail"
                name="taskRemarkDetail"
                class="layui-textarea"
                autocomplete="off"
                disabled
              />
            </div>
          </div>
          <div class="layui-col-md6">
            <label class="layui-form-label">完成备注：</label>
            <div class="layui-input-block">
              <textarea
                id="endRemarkDetail"
                name="endRemarkDetail"
                class="layui-textarea"
                autocomplete="off"
                disabled
              />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">提交人：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="createNameDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">提交时间：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="createTimeDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">审核结果：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="examineTypeNameDetail" disabled />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">审核时间：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="examineTimeDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 98px"
              >认领记录编号：</label
            >
            <div
              class="layui-input-block"
              style="width: 74%;
    margin-left: 128px;"
            >
              <input
                class="layui-input"
                style="color: red"
                id="claimNoDetail"
                disabled
              />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">认领时间：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="claimTimeDetail" disabled />
            </div>
          </div>
        </div>
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label">处置结果：</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input class="layui-input" id="executeStatusNameDetail" disabled/>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label" style="width: 98px"
              >处置记录编号：</label
            >
            <div
              class="layui-input-block"
              style="width: 74%;margin-left: 128px;"
            >
              <input class="layui-input" id="executeNoDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">处置人：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="executeNameDetail" disabled />
            </div>
          </div>
          <div class="layui-col-md4">
            <label class="layui-form-label">处置时间：</label>
            <div class="layui-input-block">
              <input class="layui-input" id="executeTimeDetail" disabled />
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-row">
          <div class="layui-col-md4">
            <label class="layui-form-label">状态：</label>
            <div class="layui-input-block">
              <input
                class="layui-input"
                style="color: red"
                id="statusDetail"
                disabled
              />
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button
          class="layui-btn layui-btn-primary"
          type="button"
          ew-event="closeDialog"
        >
          关闭
        </button>
      </div>
    </div>
  </form>
</script>

<!-- js部分 -->
<script>
  layui.use(
    ["layer", "form", "table", "admin", "tableTreeDj", "upload", "config"],
    function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var form = layui.form;
      var table = layui.table;
      var config = layui.config;
      var admin = layui.admin;
      var upload = layui.upload;

      //初始化下拉框
      //部门列表
      admin.initDataSelectTree(
        true,
        "user/admin/role/getTreeData",
        null,
        "departmentSearchMain",
        null,
        "部门",
        "departmentSearchMainId",
        function (data) {
          // 获取部门ID
          let departmentId = data.change[0].value;
          //赋值部门id
          $("#departmentId").val(data.change[0].value);
          $("#postIdDown").empty();
          //部门下的岗位列表
          admin.initDataSelect(
            "user/admin/role/getPostData",
            { roleId: departmentId },
            "postIdSearchClaim",
            null,
            "岗位",
            null,
            null
          );
        }
      );
      //渲染下拉
      form.render("select");

      // 渲染表格
      var insTb = table.render({
        elem: "#maintenanceTableInfo",
        url: config.base + "enterprise/device/admin/maintenance/list",
        toolbar: true,
        defaultToolbar: ["filter"],
        page: true,
        cellMinWidth: 100,
        cols: [
          [
            { align: "center", type: "numbers", title: "序号", fixed: "left" },
            {
              align: "center",
              field: "taskNo",
              sort: true,
              title: "任务编号",
              fixed: "left",
              width: "14%",
            },
            {
              align: "center",
              field: "maintenanceTypeName",
              sort: true,
              title: "维保类型",
              fixed: "left",
            },
            {
              align: "center",
              field: "taskNatureName",
              sort: true,
              title: "任务性质",
              width: "5%",
            },
            {
              align: "center",
              field: "deviceNo",
              sort: true,
              title: "设备清单编码",
            },
            {
              align: "center",
              field: "departmentName",
              sort: true,
              title: "处置部门",
            },
            {
              align: "center",
              field: "postName",
              sort: true,
              title: "处置岗位",
            },
            {
              align: "center",
              field: "taskTypeName",
              sort: true,
              title: "任务类型",
            },
            {
              align: "center",
              field: "statusName",
              sort: true,
              title: "状态",
              width: "4%",
            },
            {
              align: "center",
              field: "creatorName",
              sort: true,
              title: "录入人",
            },
            {
              align: "center",
              field: "createTime",
              sort: true,
              title: "录入时间",
            },
            {
              align: "center",
              field: "claimName",
              sort: true,
              title: "认领人",
            },
            {
              align: "center",
              field: "claimTime",
              sort: true,
              title: "认领时间",
            },
            {
              align: "center",
              field: "taskRemark",
              sort: true,
              title: "描述",
              width: "8%",
            },
            {
              align: "center",
              toolbar: "#maintenanceTableBar",
              title: "操作",
              width: 250,
              fixed: "right",
              width: "14%",
            },
          ],
        ],
        done: function (res, curr, count) {
          //移除按钮
          var menuButton = config.getMenuButton(location.hash);
        //   if (menuButton.indexOf("搜索") == -1) {
        //     $(".layui-form.toolbar").remove();
        //   }
        //   if (menuButton.indexOf("修改") == -1) {
        //     $("[lay-event='edit']").remove();
        //   }
        //   if (menuButton.indexOf("删除") == -1) {
        //     $("[lay-event='del']").remove();
        //   }
        },
      });

      // 工具条点击事件
      table.on("tool(maintenanceTableInfo)", function (obj) {
        var data = obj.data;
        if (obj.event === "edit") {
          //修改
          showEditModel(data, 1);
        } else if (obj.event === "detail") {
          //查看详情
          showDetailModel(data);
        } else if (obj.event === "claim") {
          //认领
          doClaim(data);
        } else if (obj.event === "audit") {
          //审核
          doSHModel(data);
        }
      });

      //监听排序
      table.on("sort(maintenanceTableInfo)", function (obj) {
        table.reload("maintenanceTableInfo", {
          initSort: obj,
          where: {
            sort: obj.field,
            order: obj.type,
          },
        });
      });

      // 搜索
      form.on("submit(formSubSearchLog_main)", function (data) {
        data.field.status = data.field.statusSearch_main;
        data.field.postId = data.field.postIdSearchMain;
        insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
      });

      // 重置搜索
      form.on("submit(resets)", (data) => {
        $(".lay-header").find("select").val("");
        $(".lay-header").find("input").val("");
        let field = data.field;
        for (let key in field) {
          field[key] = "";
        }
        insTb.reload({ where: field, page: { curr: 1 } }, "data");
      });

      // 添加按钮点击事件
      $("#maintenanceAdd").click(function () {
        showEditModel();
      });

      //认领
      function doClaim(data) {
        layer.confirm(
          "确定要认领吗？",
          {
            offset: "65px",
            skin: "layui-layer-admin",
          },
          function (i) {
            layer.close(i);
            layer.load(2);
            admin.req(
              "enterprise/device/admin/maintenance/claim",
              { id: data.id },
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  insTb.reload();
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "POST"
            );
          }
        );
      }

      //审核
      function doSHModel(data) {
        admin.open({
          type: 1,
          area: ["500px", "500px"],
          offset: "65px",
          title: "维保任务审核",
          content: $("#maintenanceSHForm").html(),
          success: function () {
            layui.form.render("select");
            form.on("submit(MaintenanceShFormSubmit)", function (d) {
              layer.load(2);
              d.field.id = data.id;
              admin.req(
                "enterprise/device/admin/maintenance/audit",
                d.field,
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    table.reload("maintenanceTableInfo");
                    layer.closeAll("page");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                    layer.closeAll("page");
                  }
                },
                "POST"
              );
              return false;
            });
          },
        });
      }

      // 详情弹窗
      function showDetailModel(data) {
        console.log("data", data);
        //deviceFL
        admin.open({
          type: 1,
          area: ["1500px", "90%"],
          offset: "65px",
          title: "维保任务详情",
          content: $("#maintenanceDetailForm").html(),
          success: function () {
            form.val("maintenanceDetailForm", data);
            //查询详情并且显示
            admin.reqSync(
              "enterprise/device/admin/maintenance/getMaintenanceById",
              { id: data.id },
              function (res) {
                if (res.code === 200) {
                  let info = res.data;
                  $("#taskTypeNameDetail").val(info.taskTypeName);
                  $("#taskNoDetail").val(info.taskNo);
                  $("#deviceNoDetail").val(info.deviceNo);
                  $("#deviceLXDetail").val(info.deviceTypeName);
                  $("#deviceFLDetail").val(info.categoryName);
                  $("#deviceCXDetail").val(info.workShopName);
                  $("#taskNatureDetail").val(info.taskNatureName);
                  $("#maintenanceTypeDetail").val(info.maintenanceTypeName);
                  $("#deviceLocationDetail").val(info.deviceLocation);
                  $("#departmentIdDetail").val(info.departmentName);
                  $("#postIdDetail").val(info.postName);
                  $("#endRemarkDetail").val(info.endRemark);
                  $("#taskNameDetail").val(info.taskName);
                  //实施前图片
                  if (info.preImg) {
                    $("#proImage").html(
                      '<img class="proPicPath" src="' +
                        info.preImg +
                        '" style="max-width: 183px"/>'
                    );
                    $(".proPicPath").click(function () {
                      let json = {};
                      json.attachment = info.preImg;
                      showViewAttachmentModel(json);
                    });
                  }
                  $("#totalMoney").val(info.totalMoney + "元");
                  $("#taskRemarkDetail").val(info.taskRemark);
                  $("#createNameDetail").val(info.creatorName);
                  $("#createTimeDetail").val(info.createTime);
                  $("#examineTypeNameDetail").val(info.examineTypeName);
                  $("#examineTimeDetail").val(info.examineTime);
                  $("#claimNoDetail").val(info.claimNo);
                  $("#claimTimeDetail").val(info.claimTime);
                  $("#executeNameDetail").val(info.executeName);
                  $("#executeNoDetail").val(info.executeNo);
                  $("#statusDetail").val(info.statusName);
                  //处置时间
                  $("#executeTimeDetail").val(info.executeTime);

                  //如果物料列表不为空
                  if (
                    Array.isArray(info.maintenanExecuteDetailVos) &&
                    info.maintenanExecuteDetailVos.length !== 0
                  ) {
                    console.log("data.maintenanExecuteDetailVos+不为空");
                    table.render({
                      elem: "#maintenanceExecuteDetailInfo",
                      data: info.maintenanExecuteDetailVos,
                      cellMinWidth: 100,
                      cols: [
                        [
                          { type: "numbers", title: "序号", fixed: "left" },
                          {
                            align: "center",
                            field: "materialNo",
                            sort: true,
                            title: "物料编号",
                            fixed: "left",
                          },
                          {
                            align: "center",
                            field: "materialName",
                            sort: true,
                            title: "物料名称",
                            fixed: "left",
                          },
                          {
                            align: "center",
                            field: "materialSpecs",
                            sort: true,
                            title: "物料规格",
                          },
                          {
                            align: "center",
                            field: "materialCategory",
                            sort: true,
                            title: "物料类别",
                          },
                          {
                            align: "center",
                            field: "priUnit",
                            sort: true,
                            title: "计价单位",
                          },
                          {
                            align: "center",
                            field: "onePrice",
                            sort: true,
                            title: "单价",
                          },
                          {
                            align: "center",
                            field: "consumption",
                            sort: true,
                            title: "用量",
                          },
                          {
                            align: "center",
                            field: "onePrice",
                            sort: true,
                            title: "用料金额",
                          },
                          {
                            align: "center",
                            field: "remark",
                            sort: true,
                            title: "备注描述",
                          },
                        ],
                      ],
                    });
                  } else {
                  }
                } else {
                }
              },
              "get"
            );
          },
        });
      }

      // 显示编辑弹窗
      function showEditModel(data, type) {
        admin.open({
          type: 1,
          area: "1200px",
          offset: "65px",
          title: data ? "修改维修保养任务" : "添加维修保养任务",
          content: $("#maintenanceForm").html(),
          success: function () {
            form.val("maintenanceForm", data);
            //如果时修改 显示图片
            if (data) {
              //初始化部门 以及岗位
              $("#departmentAddMainId").val(data.departmentName);
              $("#postIdAddDetail").val(data.postName);
              //初始化任务性质
              // $('#toolDeviceType').val(data.taskNature);
              //修改初始化设备清单号
              admin.reqSync(
                "enterprise/device/admin/device/getDeviceById",
                { id: data.deviceId },
                function (res) {
                  if (res.code === 200) {
                    //类型
                    $("#deviceLX").val(res.data.deviceTypeName);
                    //分类
                    $("#deviceFL").val(res.data.categoryName);
                    //产线
                    $("#deviceCX").val(res.data.workshopName);
                  } else {
                  }
                },
                "get"
              );

              //初始化这岗位信息
              admin.initDataSelect(
                "user/admin/role/getPostData",
                { roleId: data.departmentId },
                "postIdDown",
                data.postId,
                "岗位",
                null,
                null
              );

              //初始化任务名字
              $("#sbqdbh").text(data.deviceNo);
              if (data.deviceLocation) {
                $("#dwh").text("-" + data.deviceLocation);
              } else {
                $("#dwh").text("");
              }
              $("#sbmc").text("-" + data.deviceName);
              if (data.preImg) {
                $("#ProPic_path").html(
                  '<img class="proPicPath" src="' +
                    data.preImg +
                    '" style="max-width: 183px"/>'
                );
                $(".proPicPath").click(function () {
                  let json = {};
                  json.attachment = $("#preImg").val();
                  showViewAttachmentModel(json);
                });
              }
              //如果是详情 则要去掉这个上传框
              if (type === 2) {
                $("#ProPicBtn").remove();
              } else {
                $("#ProPicBtn").click(
                  uploadImg("ProPicBtn", "ProPic_path", "preImg")
                );
              }
            } else {
              $("#ProPicBtn").click(
                uploadImg("ProPicBtn", "ProPic_path", "preImg")
              );
            }
            //设置默认值
            form.render();
            //选择框事件

            $("#deviceNo").click(() => {
              admin.formOpen({
                area: ["90%", "90%"],
                offset: "25px",
                title: "选择设备",
                path: "components/device/device.html",
                success: function () {
                  parentData.eventName = "radio";
                },
                end: () => {
                  if (childData.list && childData.list.length > 0) {
                    let obj = childData.list[0];
                    // console.log(123, obj);
                    admin.reqSync(
                      "enterprise/device/admin/device/getDeviceById",
                      { id: obj.id },
                      function (res) {
                        if (res.code === 200) {
                          console.log("data", res.data);
                          $("#sbqdbh").text(res.data.deviceCode);
                          $("#sbmc").text("-" + res.data.name);
                          //类型
                          $("#deviceLX").val(res.data.deviceTypeName);
                          //分类
                          $("#deviceFL").val(res.data.categoryName);
                          //产线
                          $("#deviceCX").val(res.data.workshopName);
                          $("#deviceNo").val(res.data.deviceCode);
                          $("#deviceId").val(res.data.id);
                          $("#deviceModelId").val(res.data.deviceModelId);
                          $("#deviceModelName").val(res.data.deviceModelName);
                        } else {
                        }
                      },
                      "get"
                    );
                  }
                },
              });
            });
            // form.on('select(deviceNo)', function (data) {
            //     //拿到值
            //     let deviceId = data.value;
            //     admin.reqSync('enterprise/device/admin/device/getDeviceById', {id: deviceId}, function (res) {
            //         if (res.code === 200) {
            //             console.log("data", res.data);
            //             $('#sbqdbh').text(res.data.deviceCode);
            //             $('#sbmc').text("-" + res.data.name);
            //             //类型
            //             $('#deviceLX').val(res.data.deviceTypeName);
            //             //分类
            //             $('#deviceFL').val(res.data.categoryName);
            //             //产线
            //             $('#deviceCX').val(res.data.workshopName);
            //         } else {

            //         }
            //     }, 'get');
            // });

            let maintenanceType = "";
            //维保类型选择
            form.on("select(maintenanceType)", function (data) {
              maintenanceType = data.value;
              //查询当前类型的部门和岗位
              admin.reqSync(
                "enterprise/device/admin/maintenanceType/list",
                { id: maintenanceType },
                function (res) {
                  if (res.code === 200) {
                    $("#departmentAddMainId").val(res.data[0].departmentName);
                    $("#postIdAddDetail").val(res.data[0].postName);
                  }
                },
                "get"
              );
            });

            form.on("select(taskNature)", function (data) {
              taskNature = data.value;
            });

            //点位的改变时间
            $("#deviceLocation").bind("input propertychange", function () {
              if ($("#deviceLocation").val()) {
                $("#dwh").text("-" + $("#deviceLocation").val());
              } else {
                $("#dwh").text("");
              }
            });

            //下拉框改变时间
            form.on("select(taskNature)", function (data) {
              admin.reqSync(
                "enterprise/device/admin/maintenanceType/getAll",
                { taskNature: data.value },
                function (res) {
                  if (res.code === 200) {
                    loadSelectHtml(
                      res.data,
                      "请选择",
                      "maintenanceType",
                      '[lay-filter="maintenanceForm"]',
                      3
                    );
                  } else {
                    loadSelectHtml(
                      [],
                      "请选择",
                      "maintenanceType",
                      '[lay-filter="maintenanceForm"]',
                      3
                    );
                  }
                },
                "get"
              );
            });

            // 表单提交事件
            form.on("submit(maintenanceTypeFormSubmit)", function (d) {
              layer.load(2);
              let sbqdbh = $("#sbqdbh").text();
              let dwh = $("#dwh").text();
              let sbmc = $("#sbmc").text();
              let rwlx = $("#rwlx").text();
              //任务名称
              d.field.taskName = sbqdbh + dwh + sbmc + rwlx;
              d.field.maintenanceType = maintenanceType;
              d.field.deviceId = d.field.deviceId;
              d.field.deviceModelId = $("#deviceModelId").val();
              d.field.deviceModelName = $("#deviceModelName").val();
              //任务类型 默认零时任务
              d.field.taskType = 1;

              if (data) {
                d.field.status = data.status;
              } else {
                //新增默认状态1
                d.field.status = 1;
              }
              let params = {
                ...d.field,
              };
              //新增修改
              admin.req(
                data
                  ? "enterprise/device/admin/maintenance/update"
                  : "enterprise/device/admin/maintenance/add",
                JSON.stringify(params),
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    table.reload("maintenanceTableInfo");
                    layer.closeAll("page");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                data ? "PUT" : "POST",
                true,
                "application/json; charset=utf-8"
              );
              return false;
            });
          },
        });

        //查询设备清单号
        admin.reqSync(
          "enterprise/device/admin/device/getAll",
          null,
          function (res) {
            if (res.code === 200) {
              loadSelectHtml(
                res.data,
                "请选择",
                "deviceNo",
                '[lay-filter="maintenanceForm"]',
                1
              );
              if (data) {
                //修改辅助任务性质
                $("#deviceNoShowEdit").val(data.deviceId);
                layui.form.render("select");
              }
            } else {
              loadSelectHtml(
                [],
                "请选择",
                "deviceNo",
                '[lay-filter="maintenanceForm"]',
                1
              );
            }
          },
          "get"
        );

        //任务性质
        admin.reqSync(
          "enterprise/dict/admin/dictLabel/select",
          { dictType: "task_nature" },
          function (res) {
            if (res.code === 200) {
              loadSelectHtml(
                res.data,
                "请选择",
                "taskNature",
                '[lay-filter="maintenanceForm"]',
                2
              );
              if (data != null) {
                console.log("data----->", data);
                $("#toolDeviceType").val(data.taskNature);
                form.render("select");
              }
            } else {
              loadSelectHtml(
                [],
                "请选择",
                "taskNature",
                '[lay-filter="maintenanceForm"]',
                2
              );
            }
          },
          "get"
        );

        let taskNature = $("#taskNature").val();
        //查询维保类型
        admin.reqSync(
          "enterprise/device/admin/maintenanceType/getAll",
          { taskNature: taskNature },
          function (res) {
            if (res.code === 200) {
              loadSelectHtml(
                res.data,
                "请选择",
                "maintenanceType",
                '[lay-filter="maintenanceForm"]',
                3
              );
              if (data != null) {
                $("#maintenanceType").val(data.maintenanceType);
                form.render("select");
              }
            } else {
              loadSelectHtml(
                [],
                "请选择",
                "maintenanceType",
                '[lay-filter="maintenanceForm"]',
                3
              );
            }
          },
          "get"
        );

        //部门列表
        admin.initDataSelectTree(
          true,
          "user/admin/role/getTreeData",
          null,
          "departmentSearchMainId",
          data ? data.departmentId : null,
          "部门",
          "departmentId",
          function (data) {
            // 获取部门ID
            let departmentId = data.change[0].value;
            //赋值部门id
            $("#departmentId").val(data.change[0].value);
            $("#postIdDown").empty();
            //部门下的岗位列表
            admin.initDataSelect(
              "user/admin/role/getPostData",
              { roleId: departmentId },
              "postIdSearchClaim",
              null,
              "岗位",
              null,
              null
            );
          }
        );

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, formName, type) {
          let html = " <option value>" + tips + "</option>";
          if (!data || data == null) {
            $("form" + formName + " select[name='" + htmltarget + "']").html(
              html
            );
            form.render("select");
            return;
          }
          for (let i = 0; i < data.length; i++) {
            if (type == 1) {
              html += `<option value='${data[i].id}' data='${JSON.stringify(
                data[i]
              )}'>${data[i].deviceCode}</option>`;
            } else if (type == 2) {
              html += `<option value='${
                data[i].labelValue
              }' data='${JSON.stringify(data[i])}'>${
                data[i].labelName
              }</option>`;
            } else if (type == 3) {
              html += `<option value='${data[i].id}' data='${JSON.stringify(
                data[i]
              )}'>${data[i].name}</option>`;
            }
          }
          $("form" + formName + " select[name='" + htmltarget + "']").html(
            html
          );
          form.render("select");
        }
      }

      function showViewAttachmentModel(data) {
        admin.open({
          type: 1,
          area: ["60%", "80%"],
          offset: "65px",
          title: "查看附件",
          content: $("#viewAttachmentHtml").html(),
          success: function () {
            if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
              PDFObject.embed(data.attachment, "#example1");
            } else {
              var attachmentHtml = "";
              data.attachment.split(",").forEach((attachmentUrl, index) => {
                attachmentHtml +=
                  '<div><div><img src="' + attachmentUrl + '"></div></div>';
              });
              $("#example1").html(attachmentHtml);
            }
          },
        });
      }
      /**
       * 统一文件上传
       * @param _elem
       * @param pathId
       * @param inputId
       */
      function uploadImg(_elem, pathId, inputId) {
        upload.render({
          elem: "#" + _elem,
          url: config.upload_url + "uploadFile",
          data: {},
          accept: "file",
          exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx",
          headers: { Authorization: "Bearer " + config.getToken() },
          done: function (res) {
            if (res.code == 200) {
              layer.msg(res.msg || "上传成功", { icon: 1 });
              let imagUrl = res.data.url;
              $("#" + pathId).html(
                '<img class="proPicPath"  src="' +
                  imagUrl +
                  '" style="max-width: 183px"/>'
              );
              $("#" + inputId).val(imagUrl);
              $(".proPicPath").click(function () {
                let json = {};
                json.attachment = imagUrl;
                showViewAttachmentModel(json);
              });
            } else if (res.code == 502) {
              return location.replace("admin/login.html");
            } else {
              layer.msg(res.msg || "上传失败", { icon: 2 });
            }
          },
        });
      }

      // 删除
      function doDelete(obj) {
        layer.confirm(
          "确定要删除吗？",
          {
            offset: "65px",
            skin: "layui-layer-admin",
          },
          function (i) {
            layer.close(i);
            layer.load(2);
            admin.req(
              "enterprise/device/admin/maintenanceType/" + obj.data.id,
              {},
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  layer.msg(res.msg, { icon: 1 });
                  obj.del();
                } else {
                  layer.msg(res.msg, { icon: 2 });
                }
              },
              "DELETE"
            );
          }
        );
      }
    }
  );
</script>
